var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,   //是否显示面板指示点（轮播图）
    autoplay: true,         //是否自动切换（轮播图）
    interval: 6000,         //自动切换时间间隔（轮播图）
    duration: 1000,         //滑动动画时长（轮播图）
    firstCategory: '',      //一级分类集合
    firstCategoryId: '',    //一级分类id标识
    jdBanners : '',         //京东广告位
    goodsList : '',         //商品集合
    orderType : '',         //排序 字段
    startFlag : 2,         //分页标识
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.getFirstCategory();  //一级分类
    that.getbanners();        //获取京东轮播图
    that.getHeight();         //获取 高度
    

  },

  /**
   * 获取 高度
   */
  getHeight:function(){
    var that = this;
    //获取屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          jdGoodsHeght: res.windowHeight
        });
      }
    });
  },

  /**
   * 一级分类
   */
  getFirstCategory: function () {
    var that = this;
    wx.request({
      url: apiHost + "/mobile/jd/categorys", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          that.setData({
            firstCategory: res.data.data,
            firstCategoryId: res.data.data[0].codeid
          })
          that.getGoodsList(that.data.firstCategoryId, '','1','10');
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 分类点击事件
   */
  categoryFri: function (e) {
    var that = this;
    var cateId = e.currentTarget.dataset.cateid;
    that.setData({
      firstCategoryId: cateId,
      orderType: ''
    })
    that.getGoodsList(cateId, '','1','10');
  },

  /**
   * 获取京东轮播图
   */
  getbanners :function(){
    var that = this;
    wx.request({
      url: apiHost + "/mobile/jd/banners", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          that.setData({
            jdBanners: res.data.data
          })
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 排序 点击事件
   */
  orderOnclick : function(e){
    var that = this;
    var orderType = e.currentTarget.dataset.ordertype;
    var firstCategoryId = that.data.firstCategoryId;

    that.setData({
      orderType: orderType
    })
    that.getGoodsList(firstCategoryId, orderType, '1', '10');
  },

  /**
   * 获取 商品
   */
  getGoodsList: function (categoryId, orderType,start,length){
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/goods/list", //仅为示例，并非真实的接口地址
      data: {
        categoryCode: categoryId,
        orderType: orderType,
        start: start,
        length: length
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var goodsData = res.data.data.list;

          if (start > 1){
            var goodsLists = that.data.goodsList;

            // 追加数据
            for (var i = 0; i < goodsData.length; i++) {
              goodsLists.push(goodsData[i])
            }
            start = parseInt(start) + 1;
          }else{
            var goodsLists = goodsData;
            start = 2;
          }

          that.setData({
            goodsList: goodsLists,
            startFlag: start
          })
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 京东商品下拉
   */
  searchScrollLower: function () {
    var that = this;

    var firstCategoryId = that.data.firstCategoryId;
    var orderType = that.data.orderType;
    var start = that.data.startFlag;

    that.getGoodsList(firstCategoryId, orderType, start, '10');
  },


  /**
   * 跳珠京东商品详情
   */
  goJdGoodsDetail:function(e){
    
    var that = this;
    var goodsId = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: "../../goods/jdGoodsDetail/jdGoodsDetail?id=" + goodsId,
    })
  },

  /***
   * 搜索商品  跳搜索订单页
  */
  searchInput: function (e) {
    var value = e.detail.value;
    this.goPage('../../goods/searchgoods/searchgoods?value=' + value)
  },

  /**
 * 跳转页面
 */
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();
      }
    })
  },

  /**
   * 提示信息
   */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})